package gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;

/**
 * Class representing tray icon of aplication.
 */
public class RSSHunterTray extends TrayIcon {

    public RSSHunterTray(MainFrame parent) {
        super(new ImageIcon("Icons/rssicon.png").getImage(), "RSSHunter");
        this.parent = parent;
        tray = SystemTray.getSystemTray();
        initPopup();
        addMouseListener(new TrayMouseListener());
        setImageAutoSize(true);
    }

    private void initPopup() {
        popup = new PopupMenu();
        MenuItem exit = new MenuItem("Exit");
        exit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                tray.remove(RSSHunterTray.this);
                System.exit(0);
            }
        });
        popup.add(exit);
        setPopupMenu(popup);
    }

    private class TrayMouseListener extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                parent.setState(Frame.NORMAL);
                parent.setVisible(true);
            }
        }
    }
    private MainFrame parent;
    private PopupMenu popup;
    private final SystemTray tray;
}
